﻿using maxiaopeng.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace maxiaopeng.Controllers
{
    public class WeightController : ApiController
    {
        // GET: api/Weight
        [HttpGet]
        public IHttpActionResult GetList(string UserName, int page = 1, int limit = 10)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var datalist = db.UserChecks.AsEnumerable();

                if (!string.IsNullOrEmpty(UserName))
                {
                    datalist = datalist.Where(p => p.UserName.Contains(UserName));
                }

                string nowtime = DateTime.Now.ToString("yyy-MM-dd");
                DateTime time = DateTime.Parse(nowtime);
                datalist = datalist.Where(p => p.CheckinTime > time).ToList();
                datalist = datalist.Where(p => p.CheckState == "已签到").ToList();
                var count = datalist.Count();
                var list = datalist.Select(p => new {
                    Id = p.Id,
                    UserId = p.UserId,
                    UserName = p.UserName,
                    CheckInTime = p.CheckinTime.ToString(),
                    CheckState = p.CheckState,
                    CheckOutTime = p.CheckOutTime.ToString(),
                    IsCancel = p.IsCancel,
                    Weight = p.Weight
                }).Skip((page - 1) * limit).Take(limit).ToList();
                return Json(new { count = count, data = list });
            }
        }
        [HttpPost]
        public IHttpActionResult Edit([FromBody] UserChecks user)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var list = db.UserChecks.FirstOrDefault(p => p.Id == user.Id);
                list.Weight = user.Weight;
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "修改成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "修改失败！" });
                }
            }
        }
    }
}
